﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;

namespace StateGrid.WWQK
{
    /// <summary>
    /// 中文期刊数据库简单操作。
    /// </summary>
    public class WWQK_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();

        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();
        /// <summary>
        /// 获取所有外文期刊分类信息。
        /// </summary>
        /// <returns></returns>
        public IList<WWQK_FL> getAllWwqkFL()
        {
            return sqlMapper.QueryForList<WWQK_FL>("SelectAllWWQK_FL", null);
        }
        /// <summary>
        /// 获取所有外文期刊索引目录信息。
        /// </summary>
        /// <returns></returns>
        public IList<WWQK_SYML> getAllWwqkSYML()
        {
            return sqlMapper.QueryForList<WWQK_SYML>("SelectAllWWQK_SYML", null);
        }
        /// <summary>
        /// 获取所有外文期刊篇名信息。
        /// </summary>
        /// <returns></returns>
        public IList<WWQK_PM> getAllWwqkPM()
        {
            return sqlMapper.QueryForList<WWQK_PM>("SelectAllWWQK_PM", null);
        }
        /// <summary>
        /// 获取原文件名相同的所有外文期刊篇名信息。
        /// </summary>
        /// <returns></returns>
        public IList<WWQK_PM> getWwqkPMByYwjm(String ywjm)
        {
            return sqlMapper.QueryForList<WWQK_PM>("SelectWWQK_PMByYwjm", ywjm);
        }

        /// <summary>
        /// 获取一部分外文期刊分类信息。
        /// 起始结果行从1开始。最后结果包含结束行。
        /// 例如start=1,end=2，则返回第1行和第2行共2个结果。
        /// 结果排序按主键分类编号。
        /// </summary>
        /// <param name="start">起始结果行</param>
        /// <param name="end">结束结果行</param>
        /// <returns></returns>
        public IList<WWQK_FL> getSomeWwqkFL(int start, int end)
        {
            IDictionary<String, int> para = new Dictionary<String, int>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<WWQK_FL>("SelectSomeWWQK_FL", para);
        }
        /// <summary>
        /// 获取一部分外文期刊索引目录信息。
        /// 起始结果行从1开始。最后结果包含结束行。
        /// 例如start=1,end=2，则返回第1行和第2行共2个结果。
        /// 结果排序按主键索引编号。
        /// </summary>
        /// <param name="start">起始结果行</param>
        /// <param name="end">结束结果行</param>
        /// <returns></returns>
        public IList<WWQK_SYML> getSomeWwqkSYML(int start, int end)
        {
            IDictionary<String, int> para = new Dictionary<String, int>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<WWQK_SYML>("SelectSomeWWQK_SYML", para);
        }
        /// <summary>
        /// 获取一部分外文期刊篇名信息。
        /// 起始结果行从1开始。最后结果包含结束行。
        /// 例如start=1,end=2，则返回第1行和第2行共2个结果。
        /// 结果排序按主键文件名。
        /// </summary>
        /// <param name="start">起始结果行</param>
        /// <param name="end">结束结果行</param>
        /// <returns></returns>
        public IList<WWQK_PM> getSomeWwqkPM(int start, int end)
        {
            IDictionary<String, int> para = new Dictionary<String, int>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<WWQK_PM>("SelectSomeWWQK_PM", para);
        }
        /// <summary>
        /// 检索索引目录表。
        /// </summary>
        /// <param name="flbm">限定的分类编码。精确匹配。null或空字符串为不设条件。</param>
        /// <param name="wwmc">限定的索引中文名称。模糊匹配。null或空字符串为不设条件。</param>
        /// <param name="start">返回结果的起始序号。</param>
        /// <param name="end">返回结果的结束序号。</param>
        /// <returns></returns>
        public IList<WWQK_SYML> queryWwqkSYML(String flbm, String wwmc, int start, int end)
        {
            if (flbm != null)
            {
                flbm = flbm + '%';
            }
            if (wwmc != null && wwmc.Trim().Length > 0)
                wwmc = '%' + wwmc + '%';
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("start", start);
            para.Add("end", end);
            para.Add("queryFlbm", flbm);
            para.Add("queryWwmc", wwmc);
            return sqlMapper.QueryForList<WWQK_SYML>("SelectSomeWWQK_SYML", para);
        }
        /// <summary>
        /// 获取以某个条件检索索引目录表的结果总数数目。
        /// </summary>
        /// <param name="flbm">限定的分类编码。精确匹配。null或空字符串为不设条件。</param>
        /// <param name="zwmc">>限定的索引中文名称。模糊匹配。null或空字符串为不设条件。</param>
        /// <returns></returns>
        public int countQueryWwqkSYML(String flbm, String wwmc)
        {
            if (flbm != null)
            {
                flbm = flbm + '%';
            }
            if (wwmc != null && wwmc.Trim().Length > 0)
                wwmc = '%' + wwmc + '%';
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("queryFlbm", flbm);
            para.Add("queryZwmc", wwmc);
            return sqlMapper.QueryForObject<int>("CountSomeWWQK_SYML", para);
        }
        /// <summary>
        /// 获取指定索引编码的篇名。
        /// </summary>
        /// <param name="sybm">指定的索引编码。空或null表示无限制。</param>
        /// <param name="nf">指定的年份。空或null表示无限制。</param>
        /// <param name="qh">指定的期号。空或null表示无限制。</param>
        /// <param name="start">返回结果的起始序号。</param>
        /// <param name="end">返回结果的结束序号。</param>
        /// <returns></returns>
        public IList<WWQK_PM> queryWwqkPM(String sybm, String nf, String qh, int start, int end)
        {
            String wjm = null;
            if (null == sybm)
            {
            }
            else
            {
                wjm = sybm + "-";
                if (null == nf || "".Equals(nf) || null == qh || "".Equals(qh))
                {
                    wjm = wjm + "%";
                }
                else
                {
                    wjm = wjm + nf + "-" + qh + "%";
                }
            }
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("start", start);
            para.Add("end", end);
            para.Add("wjm", wjm);
            return sqlMapper.QueryForList<WWQK_PM>("SelectSomeWWQK_PM", para);
        }


        /// <summary>
        /// 模糊查询指定索引编码、年份、期号的篇名数量，文件名=索引编码-年份-期号-3位流水号.PDF
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="nf">年份</param>
        /// <param name="qh">期号</param>
        /// <returns></returns>
        public int countQueryWwqkPM(String sybm, String nf, String qh)
        {
            String wjm = null;
            if (null == sybm)
            {
            }
            else
            {
                wjm = sybm + "-";
                if (null == nf || "".Equals(nf) || null == qh || "".Equals(qh))
                {
                    wjm = wjm + "%";
                }
                else
                {
                    wjm = wjm + nf + "-" + qh + "%";
                }
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("wjm", wjm);
            return sqlMapper.QueryForObject<int>("CountSomeWWQK_PM", para);
        }
        /// <summary>
        /// 根据分类编码字符串获取分类信息。
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public WWQK_FL getWwqkFLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<WWQK_FL>("SelectWWQK_FLByFlbm", flbm);
        }

        /// <summary>
        /// 根据分类名称字符串获取分类信息。
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public WWQK_FL getWwqkFLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<WWQK_FL>("SelectWWQK_FLByFlmc", flmc);
        }
        /// <summary>
        /// 根据索引编码获取索引目录信息。
        /// </summary>
        /// <param name="sybm"></param>
        /// <returns></returns>
        public WWQK_SYML getWwqkSYMLBySybm(String sybm)
        {
            return sqlMapper.QueryForObject<WWQK_SYML>("SelectWWQK_SYMLBySybm", sybm);
        }
        /// <summary>
        /// 根据文件名获取篇名信息。
        /// </summary>
        /// <param name="wjm"></param>
        /// <returns></returns>
        public WWQK_PM getWwqkPMByWjm(String wjm)
        {
            return sqlMapper.QueryForObject<WWQK_PM>("SelectWWQK_PMByWjm", wjm);
        }
        /// <summary>
        /// 根据外文篇名获取篇名信息。
        /// </summary>
        /// <param name="wjm"></param>
        /// <returns></returns>
        public WWQK_PM getWwqkPMByWwpm(String wwpm)
        {
            return sqlMapper.QueryForObject<WWQK_PM>("SelectWWQK_PMByWwpm", wwpm);
        }
        /// <summary>
        /// 根据索引编码、年份、期号获取最大文件的篇名信息
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="nf">年份</param>
        /// <param name="qh">期号</param>
        /// <returns></returns>
        public WWQK_PM getMaxWwqkPM(String sybm, String nf, String qh)
        {
            if (null == sybm || "".Equals(sybm))
            {
                return null;
            }
            if (null == nf || "".Equals(nf))
            {
                return null;
            }
            if (null == qh || "".Equals(qh))
            {
                return null;
            }
            //设置匹配文件名
            String wjm = sybm + "-" + nf + "-" + qh + "%";
            return sqlMapper.QueryForObject<WWQK_PM>("SelectMaxWWQK_PM", wjm);
        }
        /// <summary>
        /// 根据分类编码删除分类信息。
        /// </summary>
        /// <param name="flbm">删除的行数。</param>
        public int deleteWwqkFLByFlbm(String flbm)
        {
            return sqlMapper.Delete("DeleteWWQK_FLByFlbm", flbm);
        }
        /// <summary>
        /// 根据索引编码删除索引目录信息。
        /// </summary>
        /// <param name="sybm">删除的行数</param>
        public int deleteWwqkSYMLBySybm(String sybm)
        {
            return sqlMapper.Delete("DeleteWWQK_SYMLBySybm", sybm);
        }
        /// <summary>
        /// 根据文件名删除中文期刊篇名信息。
        /// </summary>
        /// <param name="wjm">删除的行数</param>
        public int deleteWwqkPMByWjm(String wjm, String uname)
        {
            WWQK_PM wwqkPM = this.getWwqkPMByWjm(wjm);

            OperationLogItem operationLog = null;


            //切分文件名
            String[] str = wjm.Split('-');
            if (str.Length < 4)
            {
                return 0;
            }
            //文件名前缀
            String wjmStr = str[0] + "-" + str[1] + "-" + str[2] + "-";

            //目录文件名
            String startWjm = wjmStr + "000.PDF";

            int result = 0;
            //删除指定篇名及文件
            if (OperationFile.DeleteFile(wjm))
            {
                result = sqlMapper.Delete("DeleteWWQK_PMByWjm", wjm);
                operationLog = this.GetOperationLogItem(wwqkPM, "DELETE", uname);
                if (null != operationLog)
                {
                    this.operationLogDAO.insertLogItem(operationLog);
                }

            }

            ICollection<WWQK_PM> list = this.queryWwqkPM(str[0], str[1], str[2], 1, 1000);

            int ph = 1;
            String phString = null;
            if (null != list)
            {
                foreach (WWQK_PM wwqk_PM in list)
                {
                    phString = FormatPH(ph++);
                    //如果是目录则调过
                    if (wwqk_PM.wjm.Equals(startWjm))
                    {
                        ph = 1;
                        continue;
                    }
                    //如果当前文件名与新文件名一样则不做处理
                    else if (wwqk_PM.wjm.Equals(wjmStr + phString))
                    {
                        continue;
                    }
                    //如果当前文件名与新文件名不一致,则更新文件名及篇名信息
                    else
                    {
                        changeWWQK_PMWjm(wwqk_PM.wjm, wjmStr + phString);
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 插入外文期刊分类信息。
        /// </summary>
        /// <param name="fl"></param>
        public void insertWwqkFL(WWQK_FL fl)
        {
            sqlMapper.Insert("InsertWWQK_FL", fl);
        }
        /// <summary>
        /// 插入外文期刊索引目录信息。
        /// </summary>
        /// <param name="syml"></param>
        public void insertWwqkSYML(WWQK_SYML syml)
        {
            sqlMapper.Insert("InsertWWQK_SYML", syml);
        }
        /// <summary>
        /// 插入外文期刊篇名信息。
        /// </summary>
        /// <param name="pm"></param>
        public void insertWwqkPM(WWQK_PM pm)
        {
            OperationLogItem operationLog = null;
            operationLog = this.GetOperationLogItem(pm, "INSERT", pm.dlr);
            if (null != operationLog)
            {
                this.operationLogDAO.insertLogItem(operationLog);
            }
            sqlMapper.Insert("InsertWWQK_PM", pm);
        }
        /// <summary>
        /// 按索引编码更新索引目录信息。
        /// 索引编码为主键。
        /// </summary>
        /// <param name="syml"></param>
        /// <returns></returns>
        public bool updateWwqkSYMLBySYBM(WWQK_SYML syml)
        {
            bool result;
            try
            {
                sqlMapper.BeginTransaction();
                sqlMapper.Update("UpdateWWQK_SYML", syml);
                sqlMapper.CommitTransaction();
                result = true;
            }
            catch
            {
                sqlMapper.RollBackTransaction();
                result = false;
            }
            return result;
        }
        /// <summary>
        /// 按分类编码更新分类信息。
        /// 分类编码为主键。
        /// </summary>
        /// <param name="fl"></param>
        /// <returns></returns>
        public bool updateWwqkFLByFLBM(WWQK_FL fl)
        {
            bool result;
            try
            {
                sqlMapper.BeginTransaction();
                sqlMapper.Update("UpdateWWQK_FL", fl);
                sqlMapper.CommitTransaction();
                result = true;
            }
            catch
            {
                sqlMapper.RollBackTransaction();
                result = false;
            }
            return result;
        }
        /// <summary>
        /// 按文件名更新篇名信息。
        /// 文件名为主键。
        /// </summary>
        /// <param name="pm"></param>
        /// <returns></returns>
        public bool updateWwqkPMByWJM(WWQK_PM pm, String uname)
        {
            OperationLogItem operationLog = null;
            operationLog = this.GetOperationLogItem(pm, "UPDATE", uname);
            bool result;
            try
            {
                sqlMapper.BeginTransaction();
                sqlMapper.Update("UpdateWWQK_PM", pm);
                sqlMapper.CommitTransaction();
                this.operationLogDAO.insertLogItem(operationLog);
                result = true;
            }
            catch
            {
                sqlMapper.RollBackTransaction();
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 修改已有的文件名。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeWWQK_PMWjm(String oldWjm, String newWjm)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldwjm", oldWjm);
            para.Add("newwjm", newWjm);
            bool result = true;
            try
            {
                sqlMapper.Update("ChangeWWQK_PM_wjm", para);
                OperationFile.ReNameFile(oldWjm, newWjm);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 验证外文期刊篇名是否已存在
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <param name="zwpm">外文篇名</param>
        /// <returns>true是验证通过，外文篇名不存在；false验证没通过，外文期刊已存在</returns>
        public Boolean ValidateWWQK_PM(String wjm, String wwpm)
        {
            int position = wjm.LastIndexOf('-');
            String wjmSubString = wjm.Substring(0, position);

            IDictionary<String, String> para = new Dictionary<String, String>();
            para.Add("wjm", wjmSubString + "%");
            para.Add("wwpm", wwpm);

            IList<WWQK_PM> list = sqlMapper.QueryForList<WWQK_PM>("ValidateWWQK_PM", para);
            if (null == list)
            {
                return true;
            }
            else
            {
                foreach (WWQK_PM wwqkPM in list)
                {
                    if (wwqkPM.wjm.Equals(wjm))
                    {
                        continue;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>3位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "00" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "0" + phString;
            }
            phString += ".PDF";
            return phString;
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zwqkPM">中文篇名信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(WWQK_PM wwqkPM, String opType, String uname)
        {
            if (null == wwqkPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "ZWQK_PM";
            //全文页数
            operationLogItem.opPageCount = wwqkPM.ys;

            operationLogItem.opObjectID = "外文期刊";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "外文期刊";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + wwqkPM.sybm + "','" + wwqkPM.wjm + "','" + wwqkPM.ywjm + "','" + wwqkPM.nf + "','" + wwqkPM.qh + "','" + wwqkPM.zwpm + "','" + wwqkPM.wwpm + "','" + wwqkPM.zz + "','" + wwqkPM.zzdw + "','" + wwqkPM.kb + "','" + wwqkPM.ys + "','" + wwqkPM.zwwz + "','" + wwqkPM.zwztc + "','" + wwqkPM.wwwz + "','" + wwqkPM.wwztc + "','" + wwqkPM.gtfl + "','" + wwqkPM.wxbs + "','" + wwqkPM.dlr + "','" + wwqkPM.gxrq + "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }
    }
}
